<template>
	<div class="container">
		<!-- 路径导航区域 -->
		<el-breadcrumb separator="/">
			<el-breadcrumb-item :to="{ path: '/main' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>咨询师管理</el-breadcrumb-item>
			<el-breadcrumb-item>咨询订单管理</el-breadcrumb-item>
		</el-breadcrumb>
		<!-- 付款分类 -->
		<div class="status"><span>状态</span>
			<el-radio-group v-model="status" @input="statusChange">
				<el-radio-button label="全部订单"></el-radio-button>
				<el-radio-button label="未付款"></el-radio-button>
				<el-radio-button label="已付款"></el-radio-button>
			</el-radio-group>
		</div>

		<!-- 搜索区域 -->
		<el-row>
			<el-form :inline="true" :model="queryData">
				<el-form-item label="ID">
					<el-input v-model="queryData.id" @clear="queryList" clearable placeholder="请输入ID"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="queryList">搜索</el-button>
					<el-button type="danger" @click="removBatch">批量删除</el-button>
				</el-form-item>
			</el-form>
		</el-row>

		<!-- 数据表格区域 -->
		<el-table :data="dataList" border style="width: 100%" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55"></el-table-column>
			<el-table-column prop="id" label="ID" width="180"></el-table-column>
			<el-table-column prop="orderTime" label="下单时间" width="180"></el-table-column>
			<el-table-column prop="price" label="价格"></el-table-column>
			<el-table-column prop="username" label="用户"></el-table-column>
			<el-table-column prop="sortName" label="类型"></el-table-column>
			<el-table-column prop="consultantName" label="咨询师"></el-table-column>
			<el-table-column prop="levelName" label="咨询师等级"></el-table-column>
			<el-table-column prop="status" label="状态">
				<template slot-scope="scope">
					<el-tag v-if="scope.row.status == 1">未付款</el-tag>
					<el-tag type="success" v-if="scope.row.status == 0">已付款</el-tag>
				</template>
			</el-table-column>
			</el-table-column>
			<el-table-column label="操作">
				<template slot-scope="scope">
					<el-button size="mini" type="danger" @click="remove(scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!-- 分页区域 -->
		<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
			:current-page="queryData.pageNum" :page-sizes="[5, 10, 15, 20]" :page-size="queryData.pageSize"
			layout="total, sizes, prev, pager, next, jumper" :total="total">
		</el-pagination>

	</div>
</template>

<script>
	export default {
		data() {
			return {
				status: '全部订单',
				// 查询参数对象
				queryData: {
					id: '',
					status: '',
					pageNum: 1,
					pageSize: 10
				},
				// 类型列表通过查询
				userArray: [],
				consultantArray: [],
				feesArray: [],
				total: 0,
				title: '新增收费', // 对话框的标题
				dataList: [], // 当前页数据列表
				dialogFormVisible: false,
				// 新增和修改数据集
				addOrUpdateData: {
					id: "",
					feesId: "",
					userId: '',
					counselorId: ""
				},
				ids: []
			}
		},
		methods: {
			handleChange(value) {
				console.log(value);
			},
			statusChange(status, queryData) {
				if (status == '未付款') {
					this.queryData.status = 1
					this.queryList()
				} else if (status == '已付款') {
					this.queryData.status = 0
					this.queryList()
				} else {
					this.queryData.status = ''
					this.queryList()
				}
			},
			handleSelectionChange(val) {
				// val是选中的数组对象, map() 方法返回一个新数组，数组中的元素为原始数组元素调用函数处理后的值。
				this.ids = val.map(item => item.id);
			},
			removBatch() {
				if (this.ids.length == 0) {
					this.$message('请选择要删除的数据!');
				} else {
					this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					}).then(() => {
						this.$ajax.delete('sysCounselorOrder?ids=' + this.ids).then(resp => {
							if (resp.code == 200) {
								this.$message.success(resp.message)
								// 重新查询
								this.queryList()
							} else {
								this.$message.error(resp.message)
							}

						})

					}).catch(() => {
						this.$message({
							type: 'info',
							message: '已取消删除'
						});
					});
				}
			},
			// 每页条目数改变的回调
			handleSizeChange(val) {
				// 修改查询参数的pageSize
				this.queryData.pageSize = val;
				// pageSize改变，默认查询第一页
				this.queryData.pageNum = 1;
				// 重新查询
				this.queryList()
			},
			// 页码发生改变的回调
			handleCurrentChange(val) {
				// 修改查询参数的pageNum
				this.queryData.pageNum = val;
				// 重新查询
				this.queryList()
			},
			// 封装查询用户列表数据的函数
			queryList() {
				// 因为自定义axios已经定义了baseUrl，所以这里直接写相对路径即可
				this.$ajax.get('sysCounselorOrder/page', {
					params: this.queryData
				}).then(resp => {
					if (resp.code == 200) {
						this.dataList = resp.data.list
						this.total = resp.data.total
					} else {
						this.$message.error('订单数据加载失败')
					}
				})
			},
			// 对话框的取消按钮事件函数: 清空对话的数据
			cancle() {
				this.dialogFormVisible = false
				this.addOrUpdateData = {
					id: "",
					feesId: "",
					userId: '',
					counselorId: ""
				}
			},
			// 对话框的确定按钮事件函数
			confirm() {
				this.dialogFormVisible = false

				if (this.addOrUpdateData.id == "") { // 执行新增请求
					this.$ajax.post('sysCounselorOrder', this.addOrUpdateData).then(resp => {
						if (resp.code == 200) {
							this.$message.success(resp.message)
							// 重新查询
							this.queryList()
						} else {
							this.$message.error(resp.message)
						}
					})
				} else {
					this.$ajax.put('sysCounselorOrder', this.addOrUpdateData).then(resp => {
						if (resp.code == 200) {
							this.$message.success(resp.message)
							// 重新查询
							this.queryList()
						} else {
							this.$message.error(resp.message)
						}
					})
				}
				this.cancle()
			},
			// 删除用户
			remove(row) {
				this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					this.$ajax.delete('sysCounselorOrder/' + row.id).then(resp => {
						if (resp.code == 200) {
							this.$message.success(resp.message)
							// 重新查询
							this.queryList()
						} else {
							this.$message.error(resp.message)
						}

					})

				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除'
					});
				});
			},
		},
		// 页面初始化的时候，加载第一页的数据
		mounted() {
			this.queryList()
		}
	}
</script>

<style scoped>
	.status {
		margin-bottom: 20px;
	}

	.status span {
		margin-right: 15px;
	}

	.el-breadcrumb {
		margin-bottom: 15px;
		border-bottom: 1px solid #eee;
		padding-bottom: 15px;
	}

	.el-pagination {
		margin-top: 15px
	}


	.el-col>.el-select {
		width: 100%;
	}
</style>